package com.supor.aiot.basket.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.supor.aiot.basket.R;
import com.supor.aiot.basket.api.BasketImpl;
import com.supor.aiot.basket.bean.IngredientBuyBean;
import com.supor.aiot.basket.bean.IngredientCommonBean;

import java.util.List;


public class IngredientDetailBuyedAdapter extends RecyclerView.Adapter<IngredientDetailBuyedAdapter.BasketHolder>{
    private List<IngredientCommonBean> mMainList;
    private IngredientDetailAdapter.OnIngredientChangeListener listener;
    private Context mContext;

    public IngredientDetailBuyedAdapter(List<IngredientCommonBean> list, Context mContext) {
        this.mContext = mContext;
        this.mMainList = list;
    }

    public void setListener(IngredientDetailAdapter.OnIngredientChangeListener listener){
        this.listener = listener;
    }

    @NonNull
    @Override
    public IngredientDetailBuyedAdapter.BasketHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_list_detail,parent, false);
        return new IngredientDetailBuyedAdapter.BasketHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull BasketHolder holder, int position) {
        IngredientCommonBean ingredients = mMainList.get(position);
        holder.cbForAll.setChecked((ingredients.isSelected()));
        holder.cbForAll.setOnCheckedChangeListener((compoundButton, b) -> {
            changeIngredientState(ingredients.getAutoId()+"",b?1:0);
        });

        if (!TextUtils.isEmpty(ingredients.getIngredientsName()))holder.tvBasketTitle.setText(ingredients.getIngredientsName());
        if (!TextUtils.isEmpty(ingredients.getIngredientQuantity())&&!TextUtils.isEmpty(ingredients.getIngredientUnit()))holder.tvBasketNum.setText(ingredients.getIngredientQuantity()+ingredients.getIngredientUnit());
        //if (!TextUtils.isEmpty(ingredients.getRecipes()))holder.tvDegree.setText(ingredients.getRecipes());

    }


    @SuppressLint("AutoDispose")
    private void changeIngredientState(String id, int state){
        IngredientBuyBean buyBean = new IngredientBuyBean();
        buyBean.setAutoId(id);
        buyBean.setIsBuy(state);

        BasketImpl.getInstance().changeIngredientBuy(buyBean).subscribe(result -> {
            if (result.isSuccess()) {
                if (listener!=null)listener.OnStateChanged();
            } else {
                Log.e("nail", "state error");
            }
        }, throwable -> {
            Log.e("nail", "state查询失败: " + throwable.getMessage());
        });
    }

    @Override
    public int getItemCount() {
        return mMainList.size();
    }

    public class BasketHolder extends RecyclerView.ViewHolder{
        private CheckBox cbForAll;
        private TextView tvBasketTitle;
        private TextView tvBasketNum;
        private TextView tvDegree;

        public BasketHolder(@NonNull View itemView) {
            super(itemView);
             cbForAll = itemView.findViewById(R.id.cb_for_all);
             tvBasketTitle = itemView.findViewById(R.id.tv_ingredient_title);
             tvBasketNum = itemView.findViewById(R.id.tv_ingredient_sum);
             tvDegree = itemView.findViewById(R.id.tv_basket_name);
        }
    }

}
